﻿using System;
using Creventive.Wpf.ShellFactory.Services;
using Creventive.Wpf.ShellFactory.Workspace.Primitives;

namespace Creventive.Wpf.ShellFactory.Workspace
{
    /// <summary>
    /// A view model source retrieving a view model by using a service container and a service type.
    /// </summary>
    public class TypeViewModelSource : IViewModelSource
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeViewModelSource"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        public TypeViewModelSource(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");
            this.Type = type;
        }

        /// <summary>
        /// Gets the service type to retrieve.
        /// </summary>
        /// <value>The type.</value>
        public Type Type { get; private set; }

        /// <summary>
        /// Creates a view model using the specified services and for the specified view.
        /// </summary>
        /// <param name="services">The services.</param>
        /// <param name="ownerView">The owner view.</param>
        /// <returns></returns>
        public ApplicationViewModel Create(IServiceContainer services, ApplicationView ownerView)
        {
            return (ApplicationViewModel)services.GetService(this.Type);
        }
    }
}